package com.example.util.tableshard;

public class Hash {

    public static void main(String[] args) {
        System.out.println("=== Hash取模数学原理 ===");

        // 基本原理
        String key = "user123";
        int tableCount = 8;

        int hash = key.hashCode();
        int tableIndex = Math.abs(hash) % tableCount;

        System.out.printf("Key: %s%n", key);
        System.out.printf("Hash code: %d%n", hash);
        System.out.printf("Absolute: %d%n", Math.abs(hash));
        System.out.printf("Table index: %d %% %d = %d%n", Math.abs(hash), tableCount, tableIndex);

        // 分布均匀性原理
        System.out.println("\n分布均匀性原理:");
        System.out.println("  - 好的Hash函数应该产生均匀分布的hash值");
        System.out.println("  - 取模运算将hash值映射到固定范围");
        System.out.println("  - 质数模数可以减少冲突");
    }
}
